package com.chenshi.rabbitmq;

import com.chenshi.rabbitmq.RoutingEnum;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author chenyq
 * @date 2023/12/12
 */

@Configuration
public class RabbitmqConfig {

    /*
    * 简单模式的常量
    * */
    public static final String SIMPLE_QUEUE = "simple_Queue";

    /*
    * 发布/订阅模式的常量
    * */
    public static final String FANOUT_QUEUE_1 = "fanout_Queue_1";
    public static final String FANOUT_QUEUE_2 = "fanout_Queue_2";
    public static final String FANOUT_EXCHANGE = "fanout_Exchange";

    /*
    * 路由模式的常量
    * */
    public static final String DIRECT_QUEUE_1 = "direct_Queue_1";
    public static final String DIRECT_QUEUE_2 = "direct_Queue_2";
    public static final String DIRECT_EXCHANGE = "direct_Exchange";

    /*
    * 通配符模式的常量
    * */
    public static final String TOPIC_QUEUE_1 = "topic_queue_1";
    public static final String TOPIC_QUEUE_2 = "topic_queue_2";
    public static final String TOPIC_QUEUE_3 = "topic_queue_3";
    public static final String TOPIC_EXCHANGE = "topic_exchange";


    /*
    * 简单模式=====================================================================================================
    * */
    @Bean(SIMPLE_QUEUE)
    public Queue simpleQueue(){
        return new Queue(SIMPLE_QUEUE,true,false,false,null);
    }

    /*
    * 发布/订阅模式================================================================================================
    * */
    @Bean(FANOUT_QUEUE_1)
    public Queue fanout_Queue_1(){
        return new Queue(FANOUT_QUEUE_1,true,false,false,null);
    }

    @Bean(FANOUT_QUEUE_2)
    public Queue fanout_Queue_2(){
        return new Queue(FANOUT_QUEUE_2,true,false,false,null);
    }

    @Bean(FANOUT_EXCHANGE)
    public Exchange fanout_exchange(){
        //在boot中不写 autodelete 就是默认为false
        return ExchangeBuilder.fanoutExchange(FANOUT_EXCHANGE).durable(true).build();
    }

    @Bean
    public Binding bind_Fanout_queue_1(@Qualifier(FANOUT_QUEUE_1) Queue queue,
                                     @Qualifier(FANOUT_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    @Bean
    public Binding bind_Fanout_queue_2(@Qualifier(FANOUT_QUEUE_2) Queue queue,
                                     @Qualifier(FANOUT_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    /*
    * 路由模式==================================================================================================
    * */
    @Bean(DIRECT_QUEUE_1)
    public Queue direct_queue_1(){
        return new Queue(DIRECT_QUEUE_1,true,false,false,null);
    }

    @Bean(DIRECT_QUEUE_2)
    public Queue direct_queue_2(){
        return new Queue(DIRECT_QUEUE_2,true,false,false,null);
    }

    @Bean(DIRECT_EXCHANGE)
    public Exchange direct_exchange(){
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();
    }

    @Bean
    public Binding bind_direct_queue_1_error(@Qualifier(DIRECT_QUEUE_1) Queue queue,
                                       @Qualifier(DIRECT_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.ERROR).noargs();
    }

    @Bean
    public Binding bind_direct_queue_2_error(@Qualifier(DIRECT_QUEUE_2) Queue queue,
                                       @Qualifier(DIRECT_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.ERROR).noargs();
    }

    @Bean
    public Binding bind_direct_queue_2_info(@Qualifier(DIRECT_QUEUE_2) Queue queue,
                                       @Qualifier(DIRECT_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.INFO).noargs();
    }

    @Bean
    public Binding bind_direct_queue_2_warning(@Qualifier(DIRECT_QUEUE_2) Queue queue,
                                       @Qualifier(DIRECT_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.WARNING).noargs();
    }

    /*
    * 通配符模式==================================================================================================
    * */
    @Bean(TOPIC_QUEUE_1)
    public Queue topic_queue_1(){
        return new Queue(TOPIC_QUEUE_1,true,false,false,null);
    }

    @Bean(TOPIC_QUEUE_2)
    public Queue topic_queue_2(){
        return new Queue(TOPIC_QUEUE_2,true,false,false,null);
    }

    @Bean(TOPIC_QUEUE_3)
    public Queue topic_queue_3(){
        return new Queue(TOPIC_QUEUE_3,true,false,false,null);
    }

    @Bean(TOPIC_EXCHANGE)
    public Exchange topic_exchange(){
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }

    @Bean
    public Binding bind_topic_queue_1_caiwubu(@Qualifier(TOPIC_QUEUE_1) Queue queue,
                                      @Qualifier(TOPIC_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.CAIWUDIRECT).noargs();
    }

    @Bean
    public Binding bind_topic_queue_2_renshibu(@Qualifier(TOPIC_QUEUE_2) Queue queue,
                                      @Qualifier(TOPIC_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.RENSHIMANAGER_SICHUAN).noargs();
    }

    @Bean
    public Binding bind_topic_queue_2_caiwubu_taiyuan(@Qualifier(TOPIC_QUEUE_3) Queue queue,
                                      @Qualifier(TOPIC_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RoutingEnum.CAIWUMANAGER_TAIYUAN).noargs();
    }






}
